﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using thinger.VisionModels;
using static thinger.VisitionDAL.MtcpH5U;

namespace thinger.VisitionDAL
{
    public class H5U
    {
        public bool connectedH5UOK = false;
        public bool H5UStop = false;
        int nNetId = 0;
        public OperateResult OpenH5U(string ipAddress)
        {
            int nIpPort = 502;
            try
            {
                bool result = MtcpH5U.Init_ETH_String(ipAddress, nNetId, nIpPort);
                if (result)
                {
                    connectedH5UOK = true;

                    return new OperateResult()
                    {
                        IsSuccess = true,
                        Message = "PLC连接成功！"
                    };
                }
                else
                {
                    return new OperateResult()
                    {
                        IsSuccess = false,
                        Message = "连接失败：IP或端口号不正确！"
                    };
                }
            }
            catch (Exception)
            {

                return new OperateResult()
                {
                    IsSuccess = false,
                    Message = "连接失败：IP或端口号不正确！"
                };
            }
         
        }



        public OperateResult CloseH5U()
        {
            if (!connectedH5UOK)
            {
                return new OperateResult()
                {
                    IsSuccess = true,
                    Message = "PLC关闭成功！"
                };
            }


           
            bool result = MtcpH5U.Exit_ETH(nNetId);
            if (result)
            {
                connectedH5UOK = true;

                return new OperateResult()
                {
                    IsSuccess = true,
                    Message = "PLC关闭成功！"
                };
            }
            else
            {
                return new OperateResult()
                {
                    IsSuccess = false,
                    Message = "PLC关闭失败！"
                };
            }
        }


        public OperateResult WriteH5U(int nStartAddr,string nValueStr)
        {

            ushort nValue = ((ushort)Convert.ToInt32(nValueStr));
            //代码示例1：把65534写入D100中
            if (!connectedH5UOK)
            {
                return new OperateResult()
                {
                    IsSuccess = false,
                    Message = "PLC未连接！"
                };
            }

            {
                //int nStartAddr = 100;//寄存器地址
                int nCount = 1;//寄存器个数
                byte[] pValue = new byte[2];//缓冲区
                //ushort nValue = 65534;

                //把要写的数据存入缓冲区，备写
                pValue[0] = (byte)(nValue % 256);
                pValue[1] = (byte)(nValue / 256);

                //调用api写数据
                int nRet = MtcpH5U.H5u_Write_Soft_Elem(SoftElemType.REGI_H5U_D, nStartAddr, nCount, pValue, nNetId);

                if (1 == nRet)
                {
                    return new OperateResult()
                    {
                        IsSuccess = true,
                        Message = "写寄存器成功！"
                    };
                }
                else
                {
                    return new OperateResult()
                    {
                        IsSuccess = false,
                        Message = "写寄存器失败！"
                    };
                }
            }
        }


        public OperateResult ReadH5U(int nStartAddr = 100)
        {

            if (!connectedH5UOK)
            {
                return new OperateResult()
                {
                    IsSuccess = false,
                    Message = "PLC未连接！"
                };
            }
            //缓冲区。读取整型字元件则一个元件占两个byte，位元件则占一个byte。例如读取D元件500个，则缓冲区大小为1000以上
            byte[] pBuf = new byte[16000];
            int nCount = 1;
            bool bIsWord = false;//是否字元件

            SoftElemType ElemType = SoftElemType.REGI_H5U_D;


            int nRet = MtcpH5U.H5u_Read_Device_Block(ElemType, nStartAddr, nCount, pBuf, nNetId);

            if (nRet != 1)
            {
                return new OperateResult()
                {
                    IsSuccess = false,
                    Message = "读寄存器失败！"
                };
            }
            string strData = "";
            int nDataType = 0;
            if (nDataType == 1 || nDataType == 2)
            {
                nCount = nCount / 2;
            }
            for (int i = 0; i < nCount; i++)
            {
                if (bIsWord)
                {
                    if (nDataType == 0)//16位整形
                    {
                        byte[] databuf = new byte[2] { 0, 0 };
                        databuf[0] = pBuf[i * 2];
                        databuf[1] = pBuf[i * 2 + 1];
                        int iTemp = BitConverter.ToInt16(databuf, 0);
                        strData = strData + iTemp.ToString() + " ";
                        continue;
                    }
                    else if (nDataType == 1)//读取32位整形
                    {
                        byte[] databuf = new byte[4] { 0, 0, 0, 0 };
                        databuf[0] = pBuf[i * 4];
                        databuf[1] = pBuf[i * 4 + 1];
                        databuf[2] = pBuf[i * 4 + 2];
                        databuf[3] = pBuf[i * 4 + 3];
                        int iTemp = BitConverter.ToInt32(databuf, 0);
                        strData = strData + iTemp.ToString() + " ";
                        continue;
                    }
                    else if (nDataType == 2)//读取浮点型
                    {
                        byte[] databuf = new byte[4] { 0, 0, 0, 0 };
                        databuf[0] = pBuf[i * 4];
                        databuf[1] = pBuf[i * 4 + 1];
                        databuf[2] = pBuf[i * 4 + 2];
                        databuf[3] = pBuf[i * 4 + 3];
                        float fTemp = BitConverter.ToSingle(databuf, 0);
                        strData = strData + fTemp.ToString() + " ";
                        continue;
                    }
                }
                else
                {
                    int nVal = 0;
                    nVal = pBuf[i];
                    strData = strData + nVal.ToString() + " ";
                }
            }

            return new OperateResult()
            {
                IsSuccess = true,
                Message = strData.Trim()
            };
        }


    }
}
